package com.module.user.ui.goalSetting

import android.view.View
import com.common.app.base.AppPictureSelectionActivity
import com.common.app.core.RouterHub
import com.common.app.data.bean.BaseCityModel
import com.common.app.data.bean.KeyPre
import com.common.app.data.bean.user.UserGoalBean
import com.common.app.helper.RouterHelper
import com.common.app.helper.UserHelper
import com.common.app.utls.HeadTitleUtils
import com.common.base.app.extras.setOnClickListener
import com.common.base.app.extras.yes
import com.common.base.utils.*
import com.module.user.R
import com.module.user.databinding.UserActivityGoalSettingBinding
import com.module.user.ui.setting.CommonMeteringUnitSelectDialog

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2025/09/01
 * <p>
 * 描述：目标设置页面
 * <p>
 * 修订历史：
 */
class GoalSettingActivity : AppPictureSelectionActivity<UserActivityGoalSettingBinding, GoalSettingViewModel>(),
    View.OnClickListener {

    override fun getViewModel() = GoalSettingViewModel::class.java

    override fun getViewContentBinding() = UserActivityGoalSettingBinding.inflate(layoutInflater)

    override fun isShowToolBar(): Boolean = false
    override fun isStatusBarTranslate(): Boolean = true

    private val unitSelectDialog : CommonMeteringUnitSelectDialog? by lazy{CommonMeteringUnitSelectDialog(this)}

    override fun initViews() {
        super.initViews()
        StatusBarUtils.setStatusBarHeight(mViewContentBinding.statusView)
        StatusBarUtils.setStatusBarForegroundColor(this, true)

        val headTitleUtils = HeadTitleUtils(this)
        headTitleUtils.setTitle(resources.getString(R.string.user_setting_goal_setting_hint))
        headTitleUtils.setLineStyle(10)
    }

    override fun onResume() {
        super.onResume()
    }

    override fun onDestroy() {
        super.onDestroy()
    }

    override fun initEvents() {
        super.initEvents()
        mViewContentBinding.kvSleepPlan.setOnClickListener(onClick = this)
        mViewContentBinding.kvStepTarget.setOnClickListener(onClick = this)
        mViewContentBinding.kvCalorieTarget.setOnClickListener(onClick = this)
        mViewContentBinding.kvAcDurationTarget.setOnClickListener(onClick = this)

        unitSelectDialog?.setListener(object : CommonMeteringUnitSelectDialog.OnSelectListener{
            override fun onSelectClick(mType:Int, mBean: BaseCityModel) {
                setUserGoalInfo(mType, mBean.code.toInt())
            }
        })
    }

    override fun lazyInitData() {
        super.lazyInitData()

        SpUtilsManagement.getParcelable(KeyPre.KEY_GOAL_USER_BEAN, UserGoalBean::class.java)?.let {
            mViewContentBinding.kvSleepPlan.setValue("${it.sleep_duration_target.toInt()}${getString(R.string.common_hours_hint)}")
            mViewContentBinding.kvStepTarget.setValue("${it.steps_target.toInt()}${getString(R.string.common_step_splice)}")
            mViewContentBinding.kvCalorieTarget.setValue("${it.calories_target.toInt()}${getString(R.string.common_calorie_splice)}")
            mViewContentBinding.kvAcDurationTarget.setValue("${it.activity_duration_target.toInt()}${getString(R.string.common_minutes_hint)}")
        }
        getUserGoalInfo()
    }

    override fun onClick(v: View?) {
        when (v) {
            mViewContentBinding.kvSleepPlan -> unitSelectDialog?.show(101)
            mViewContentBinding.kvStepTarget -> unitSelectDialog?.show(102)
            mViewContentBinding.kvCalorieTarget -> unitSelectDialog?.show(103)
            mViewContentBinding.kvAcDurationTarget ->unitSelectDialog?.show(104)
        }
    }

    private fun getUserGoalInfo() {
        mViewModel.getUserGoalInfo().observe(this) {
            it.isSuccess().yes {
                it.Data?.let { userGoalBeanIt->
                    SpUtilsManagement.putParcelable(KeyPre.KEY_GOAL_USER_BEAN, userGoalBeanIt)
                    mViewContentBinding.kvSleepPlan.setValue("${userGoalBeanIt.sleep_duration_target.toInt()}${getString(R.string.common_hours_hint)}")
                    mViewContentBinding.kvStepTarget.setValue("${userGoalBeanIt.steps_target.toInt()}${getString(R.string.common_step_splice)}")
                    mViewContentBinding.kvCalorieTarget.setValue("${userGoalBeanIt.calories_target.toInt()}${getString(R.string.common_calorie_splice)}")
                    mViewContentBinding.kvAcDurationTarget.setValue("${userGoalBeanIt.activity_duration_target.toInt()}${getString(R.string.common_minutes_hint)}")

                    SpUtilsManagement.putInt(KeyPre.KEY_SLEEP_GOALS_VALUE, userGoalBeanIt.sleep_duration_target.toInt())
                    SpUtilsManagement.putInt(KeyPre.KEY_STEP_TARGET_VALUE, userGoalBeanIt.steps_target.toInt())
                    SpUtilsManagement.putInt(KeyPre.KEY_CALORIE_TARGET_VALUE, userGoalBeanIt.calories_target.toInt())
                    SpUtilsManagement.putInt(KeyPre.KEY_AC_DURATION_TARGET_VALUE, userGoalBeanIt.activity_duration_target.toInt())
                }
            }
        }
    }

    private fun setUserGoalInfo(metricType:Int,mValue:Int) {
        mViewModel.setUserGoalInfo(metricType, mValue).observe(this) {
            it.isSuccess().yes {
                getUserGoalInfo()
            }
        }
    }
}